package com.lineage.data.npc.quest;

import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1War;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.timecontroller.event.ranking.RankingClanTimer;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldWar;
import java.util.Iterator;
import java.util.List;

public class Npc_ClanRanking extends NpcExecutor {
  public static NpcExecutor get() {
    return new Npc_ClanRanking();
  }
  
  public int type() {
    return 3;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    String[] userName = RankingClanTimer.userName();
    String[] out = new String[20];
    int i = 0;
    while (i < userName.length) {
      if (!userName[i].equals(" ")) {
        out[i] = String.valueOf(String.valueOf(userName[i].replace(",", "("))) + ")";
        out[i + 10] = "对" + out[i] + "宣战";
      } 
      i++;
    } 
    pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "y_c_1", out));
  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
    if (pc.getClanid() == 0) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1064));
      return;
    } 
    L1Clan clan = pc.getClan();
    if (clan == null) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1064));
      return;
    } 
    if (!pc.isCrown()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(478));
      return;
    } 
    if (clan.getLeaderId() != pc.getId()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(518));
      return;
    } 
    String[] clanNameList = RankingClanTimer.userName();
    int i = -1;
    if (cmd.matches("[0-9]+"))
      i = Integer.valueOf(cmd).intValue(); 
    if (i != -1) {
      String tg = clanNameList[i];
      if (tg.equals(" "))
        return; 
      String[] set = tg.split(",");
      String tgClanName = set[0];
      L1Clan clanX = WorldClan.get().getClan(tgClanName);
      if (clanX.getCastleId() != 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "不能对佣有城堡的血盟宣战!"));
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        return;
      } 
      if (clanX.getClanId() == pc.getClanid())
        return; 
      List<L1War> warList = WorldWar.get().getWarList();
      String clanName = clan.getClanName();
      Iterator<L1War> iterator = warList.iterator();
      while (iterator.hasNext()) {
        L1War war = iterator.next();
        if (!war.checkClanInSameWar(clanName, tgClanName))
          return; 
        if (war.checkClanInWar(tgClanName)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(236, tgClanName));
          return;
        } 
      } 
      L1PcInstance enemyLeader = World.get().getPlayer(clanX.getLeaderName());
      if (enemyLeader == null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(218, tgClanName));
        return;
      } 
      enemyLeader.setTempID(pc.getId());
      enemyLeader.sendPackets((ServerBasePacket)new S_Message_YN(217, clanName, pc.getName()));
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
}
